// myself/apply/SuppliesApply.js
import api from "../../utils/api.js";
import utils from "../../utils/utils.js";
import network from "../../utils/network.js";
import formatDate from "../../utils/format_date.js";
const app = getApp();

Page({

  /**
   * 页面的初始数据
   */
  data: {
    finish: true,
    types: ['文具', '电脑耗材', '生活用品'],
    shownext:false,
    nextUsers:[],
    dataNextUser:[],
    sysnextopt:'',
    sysnextoptid:'',
    setid:107
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.toast = this.selectComponent("#toast");
    network.postJson(this, api.getNext, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      setid: this.data.setid,
    }).then(res => {
      console.log(res.data.data)
      if(res.data.data.shownext){
        let arr = [];
        var result = res.data.data.nextUser;
        for (const key in result) {
          arr.push({ id: result[key].id, username: result[key].name+" ("+result[key].ranking+")" });
        }
        this.setData({
          nextUsers:arr,
          dataNextUser:res.data.data.nextUser,
          nextName:res.data.data.nextName
        })
        
      }
      this.setData({
        shownext: res.data.data.shownext,
        sysnextcustidid:res.data.data.sysnextcustidid
      })
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  bindNextChange: function (e) {
    let applyIndex = e.detail.value
    let nextUserList = this.data.nextUsers
    let dataNextUser = this.data.dataNextUser
    console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      applyIndex:applyIndex,
      sysnextoptid:nextUserList[applyIndex].id,
      sysnextopt:dataNextUser[applyIndex].name
    })
  },
  showSheet() {
    let that = this;
    wx.showActionSheet({
      itemList: this.data.types,
      success(res) {
        that.setData({
          type: that.data.types[res.tapIndex]
        })
      }
    })

  },
  formSubmit: function (e) {
    console.log('form发生了submit事件，携带数据为：', e.detail.value)
 if (!this.data.type) {
      this.toast.showToast('请选择类型');
      return;
    } else if (!e.detail.value.name) {
      this.toast.showToast('请填写名称');
      return;
    }
    if(this.data.shownext && (!this.data.sysnextoptid || !this.data.sysnextopt)){
      this.toast.showToast('请选择经办人员');
      return;
    }
    if(this.data.shownext && this.data.sysnextcustidid == 0){
      this.toast.showToast('系统错误');
      return;
    }


    network.postJson(this, api.save, {
      adminid: app.globalData.userInfo.uid,
      token: app.globalData.userInfo.token,
      id: 0,
      type: this.data.type,
      name: e.detail.value.name,
      sysmodenum: 'bangong',
      sysmodeid: this.data.setid,
      sysnextcustidid:this.data.sysnextcustidid,
      sysnextoptid:this.data.sysnextoptid,
      sysnextopt:this.data.sysnextopt
    }).then(res => {
      this.toast.showToast('提交成功');
      wx.navigateBack();
    });
  }
})